package com.ruoyi.wx.service.imp;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.constant.ConstantWxBase;
import com.ruoyi.wx.pojo.vo.UserInfoVo;
import com.ruoyi.wx.service.WxAuthService;
import com.ruoyi.wx.service.WxUserWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class WxUserWebServiceImp implements WxUserWebService {

    @Autowired
    private WxAuthService wxAuthService;

    @Override
    public Map<String, Object> getUserList() {
        String result = HttpUtil.get(String.format(ConstantWxBase.Url.GET_USER_LIST.getUrl(), wxAuthService.adminGetAccessToken()));
        return JSON.parseObject(result, new TypeReference<Map<String, Object>>() {
        }.getType());
    }

    @Override
    public Map<String, Object> getUserInfo(List<UserInfoVo> wxOpenIds) {
        String result = HttpUtil.createPost(String.format(ConstantWxBase.Url.GET_USER_INFO.getUrl(), wxAuthService.adminGetAccessToken()))
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .body(String.format("{\"user_list\":%s}", JSON.toJSONString(wxOpenIds)))
                .execute().body();
        return JSON.parseObject(result, new TypeReference<Map<String, Object>>() {
        }.getType());
    }
}
